package com.jinyang.jetpackdemo.activity.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.jinyang.jetpackdemo.R;

/**
 * @Author: LiuJinYang
 * @CreateDate: 2021/12/24
 * @Description:
 */
public class CustomizedProgressBar extends View {

    //进度条最大值
    private float maxCount = 100;
    //进度条当前值
    private float currentCount;
    private int mWidth, mHeight;
    private Context mContext;
    private String text="";
    private float textSize=20;
    private int textColor=R.color.white;

    public CustomizedProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public CustomizedProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public CustomizedProgressBar(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint mPaint = new Paint();
        mPaint.setAntiAlias(true);
        //半径
        int round = mHeight / 2;

        int lineTop=mHeight/4;
        int lineBottom=mHeight/4*3;

        //设置边框背景颜色
        mPaint.setColor(getResources().getColor(R.color.white_alpha));
        RectF rectBg = new RectF(0, lineTop, mWidth, lineBottom);
        //绘制 最外面的大 圆角矩形，背景为白色
        canvas.drawRoundRect(rectBg, round, round, mPaint);

        //进度条的比例
        float section = currentCount / maxCount;
        RectF rectProgressBg = new RectF(0, lineTop, mWidth * section, lineBottom);

        Log.e("CustomizedProgressBar", currentCount + "");
        Log.e("CustomizedProgressBar", section + "");

        //Paint设置setColor(白色无透明)和setShader，只让setShader生效；不然前面setColor设置了透明度，透明度会生效，和setShader效果叠加
        mPaint.setColor(getResources().getColor(R.color.white));
        mPaint.setShader(getLinearGradient());
        //最左边的圆角矩形
        canvas.drawRoundRect(rectProgressBg, round, round, mPaint);


        mPaint.setShader(getLinearGradient());
        canvas.drawCircle(mWidth * section - round, round, (float) (round), mPaint);

        if (TextUtils.isEmpty(text)){
            return;
        }

        Paint paint=new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize((float) (round*0.8));
        paint.setColor(getResources().getColor(textColor));
//        mPaint.setAlpha(textAlpha);
        int offsetY=0;
        // text 居中绘制，round + offsetY 是 text 的中心坐标
        Paint.FontMetrics fm = paint.getFontMetrics();
        float baseline = round + offsetY - (fm.top + fm.bottom) / 2f;
        Rect rect=new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
        canvas.drawText(text, mWidth * section-round/4-rect.width(), baseline, paint);
    }

    private LinearGradient linearGradient;

    private LinearGradient getLinearGradient() {
        if (linearGradient == null) {
            //根据R文件中的id获取到color
            linearGradient = new LinearGradient(0, 0, getWidth(), mHeight, new int[]{mContext.getResources().getColor(R.color.progress_color_1),
                    mContext.getResources().getColor(R.color.progress_color_1)}, null, Shader.TileMode.CLAMP);
        }
        return linearGradient;
    }

    private int dipToPx(int dip) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
    }

    /***
     * 设置最大的进度值
     * @param maxCount 最大的进度值
     */
    public void setMaxCount(float maxCount) {
        this.maxCount = maxCount;
    }

    /***
     * 设置当前的进度值
     * @param currentCount 当前进度值
     */
    public void setCurrentCount(float currentCount) {
        this.currentCount = currentCount > maxCount ? maxCount : currentCount;
        invalidate();
    }

    public void setText(String text) {
        this.text = text;
        invalidate();
    }

    public float getMaxCount() {
        return maxCount;
    }

    public float getCurrentCount() {
        return currentCount;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) {
            mWidth = widthSpecSize;
        } else {
            mWidth = 0;
        }
        if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) {
            mHeight = dipToPx(18);
        } else {
            mHeight = heightSpecSize;
        }
        setMeasuredDimension(mWidth, mHeight);
    }
}
